package interfaz_usuario;

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.Action;
import javax.swing.JFileChooser;
import javax.swing.JPanel;

import universe.Universe;




public class MainPanel extends JPanel{
	
	private HeadMenuPanel headMenu;
	private PaintPanel paintPanel;
	private ObjectsBarPanel objectsBarPanel;
	
	public MainPanel(){
		setLayout(new BorderLayout());
		setVisible(true);
		
		paintPanel= new PaintPanel();
		headMenu= new HeadMenuPanel();
		objectsBarPanel= new ObjectsBarPanel();
		
		add(headMenu,BorderLayout.NORTH);
		add(paintPanel,BorderLayout.CENTER);
		add(objectsBarPanel,BorderLayout.WEST);
		
		
		headMenu.getButtonNew().getOkButton().addActionListener(new btnNewOkClicked());
		headMenu.getItemSave().addActionListener(new btnSaveClicked());
		headMenu.getItemLoad().addActionListener(new btnLoadClicked());
		
		headMenu.getItemBigBox().addActionListener(new btnGenerateSquaresClicked());
		
		objectsBarPanel.addListener(paintPanel);
		
		paintPanel.grabFocus();
		
	}

	public HeadMenuPanel getHeadMenu() {
		return headMenu;
	}

	public void setHeadMenu(HeadMenuPanel headMenu) {
		this.headMenu = headMenu;
	}

	public PaintPanel getPaintPanel() {
		return paintPanel;
	}

	public void setPaintPanel(PaintPanel paintPanel) {
		this.paintPanel = paintPanel;
	}
	
	private class btnNewOkClicked implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			System.out.println("entre aca");
			int width = Integer.parseInt(headMenu.getButtonNew().getTextWidth().getText());
			int height = Integer.parseInt(headMenu.getButtonNew().getTextHeight().getText());
			
			paintPanel.newWorld(width,height);
			
			headMenu.getButtonNew().setVisible(false);
			repaint();
			
			paintPanel.grabFocus();
				
		}
		
	}
	private class btnSaveClicked implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println("guardar");
			JFileChooser fc=new JFileChooser(System.getProperty("user.dir"));
			fc.showSaveDialog(headMenu);
			try{
			File Guardar =fc.getSelectedFile();
			  if(Guardar !=null){
				  //
				  
				System.out.println("guardar okas");
				System.out.println(paintPanel.save());
			    FileWriter  Guardx=new FileWriter(Guardar);
			    
			    Guardx.write(paintPanel.save());
			    Guardx.close(); //Cierra el fichero
			   }
			}
			catch(IOException ioe){
				 System.out.println(ioe); //Muestra por consola los errores
				} 
			
		}
		
	}
	
	private class btnLoadClicked implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			JFileChooser fc=new JFileChooser(System.getProperty("user.dir"));
			fc.showOpenDialog(headMenu);
			File load =fc.getSelectedFile();
			  if(load !=null){
			    
				  paintPanel.load(load.getName());
				
				
			   } 
			
		}
		
	}
	
	private class btnGenerateSquaresClicked implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			
			
		}
		
	}
	
}
